package com.mzy.linear_struct.hot100.链表;

/**
 * @Author: mizy3
 * @Date: 2024/09/26/上午9:46
 * @Description:
 */
public class 删除排序链表中的重复元素II {
    public ListNode deleteDuplicates(ListNode head) {
        //      d
        //        s
        // -1 1 2 3 4 4 5
        //          f
        ListNode dummy = new ListNode(-1 , head);
        ListNode ans = dummy;
        ListNode slow = dummy.next;
        ListNode fast = dummy.next.next;
        while(slow != null && fast != null){
            if(slow.val == fast.val){
                while(fast != null && slow.val == fast.val){
                    fast = fast.next;
                }
                slow.next = fast;
            }else {
                slow = slow.next;
                fast = fast.next;
            }
        }
        return ans.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(3, new ListNode(4, new ListNode(4, new ListNode(5)))))));
        System.out.println(new 删除排序链表中的重复元素II().deleteDuplicates(head));
    }
}


































